<?php
#基础函数
include_once('_path.php');
include_once("../includes/init.php");

#获取页面变量
$action = trim($_GET['action']);
$id = trim($_REQUEST['id']) + 0;
	$quote['id'] = string_sql($id);
$uid = trim($_REQUEST['uid']) + 0;

if(empty ($uid)){
    exit;
}

if ($id > 0) {
	$sql = "SELECT * FROM members_card WHERE id=$id";
	$_card = $database->queryFirst($sql);
	if (!$_card['id']) {
		$_REQUEST['referrer'] = "index.php?uid=".$uid;
		redirect("记录不存在!");
	}
}
#如果是添加操作
if ($action == "cardadd.do") {
    #已经是副卡，就不能在被绑定
    if($_card['status']=='2'){
        $_REQUEST['referrer'] = "index.php?uid=".$uid;
        redirect("该卡已被绑定为副卡!");
        exit;
    }

    $card = trim($_REQUEST['card']);$quote['card'] = string_sql($card);

    if(empty($card)){
        $_REQUEST['referrer'] = "index.php?uid=".$uid;
        redirect("卡号不能为空!");
        exit;
    }

    $sql = "INSERT INTO members_card(uid, card) VALUES (".$uid.", ".$quote['card'].");";
    $database->execute($sql);
    $sql = "UPDATE members_info SET card=".$quote['card']." WHERE id=".$uid;
    $database->execute($sql);
    $_REQUEST['referrer'] = "index.php?uid=".$uid;
    redirect("会员卡绑定成功!");
    exit;
#设置为主卡
}else if ($action == "cardbind.do" && $id > 0) {
    $sql = "UPDATE members_card SET status=1 WHERE id=".$id;
    $database->execute($sql);
    $_REQUEST['referrer'] = "index.php?uid=".$uid;
    redirect("设置为主卡成功!");
    exit;
#主卡解除绑定
}else if ($action == "cardunbind.do" && $id > 0) {
    #删除副卡
    $sql = "UPDATE members_card SET status = 0 WHERE pid=".$id;
    $database->execute($sql);
    #删除主卡
    $sql = "DELETE FROM members_card WHERE id=".$id;
    $database->execute($sql);
    #删除卡号关联
    $sql = "UPDATE members_info SET card=NULL WHERE id=".$uid;
    $database->execute($sql);
    $_REQUEST['referrer'] = "index.php?uid=".$uid;
    redirect("解除绑定成功!");
    exit;
#添加副卡cardinfoadd
} else if ($action == "cardinfoadd.do" && $id > 0) {

      $card = trim($_REQUEST['card']);$quote['card'] = string_sql($card);
      if(empty($card)){
        $_REQUEST['referrer'] = "index.php?uid=".$uid;
        redirect("卡号不能为空!");
        exit;
    }

    #副卡尚未激活
    $rs = $database->queryFirst("SELECT * FROM members_card WHERE card=".$quote['card']);
    if(empty ($rs['id'])){
        $_REQUEST['referrer'] = "index.php?uid=".$uid;
        redirect("该副卡尚未绑定会员!");
        exit;
    }elseif($rs['status']=='1'){
        $_REQUEST['referrer'] = "index.php?uid=".$uid;
        redirect("该卡已经是主卡，不能被绑定为副卡!");
        exit;
    }
    
    $sql = "UPDATE members_card SET pid =".$id.", status=2 WHERE id=".$rs['id'];
    $database->execute($sql);
    $_REQUEST['referrer'] = "index.php?uid=".$uid;
    redirect("添加副卡成功!");
    exit;
}
#解除副卡del.do
else if ($action == "delete.do" && $id > 0) {
    $sql = "UPDATE members_card SET status = 0 WHERE id=".$id;
    $database->execute($sql);
    $_REQUEST['referrer'] = "index.php?uid=".$uid;
    redirect("删除副卡成功!");
    exit;
}